package com.hengtian.roastwall.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hengtian.roastwall.common.page.PageUtil;
import com.hengtian.roastwall.entity.Comment;
import com.hengtian.roastwall.entity.LoginUser;
import com.hengtian.roastwall.entity.User;
import com.hengtian.roastwall.entity.dto.AddRoastDto;
import com.hengtian.roastwall.entity.dto.CommentDTO;
import com.hengtian.roastwall.mapper.CommentMapper;
import com.hengtian.roastwall.mapper.UserMapper;
import com.hengtian.roastwall.service.MomentsService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MomentServiceImpl  implements MomentsService   {
    @Resource
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public String imagesUpload(MultipartFile file)  {
        String fileName = file.getOriginalFilename();
        String uploadDir = "pic"; // 新的文件上传目录
        String filePath = new File("").getAbsolutePath() +File.separator+uploadDir+File.separator + fileName;
        File newFile = new File(filePath);
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "http://59.110.174.205:8081/"+uploadDir+"/" + fileName;
    }

    @Override
    public String addRoast(AddRoastDto dto) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        log.info("用户信息：{}",loginUser);
        Comment comment = new Comment();
        BeanUtils.copyProperties(dto,comment);
        comment.setUserId(loginUser.getUser().getId().toString());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = format.format(new Date());
        comment.setCreateTime(createTime);
        int insert = commentMapper.insert(comment);
        return insert == 0 ? "发布失败" : "发布成功";
    }

    @Override
    public void deleteComment(Integer commentId) {
        commentMapper.deleteById(commentId);
    }

    @Override
    public List<Comment> commentList(CommentDTO commentDTO) {
        Page<Comment> page = new Page<>(commentDTO.getPageNo(), commentDTO.getPageSize());
        // TODO: 2023/11/12 具体展示规则
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .orderByDesc(Comment::getSupportNum);
        Page<Comment> commentPage = commentMapper.selectPage(page, wrapper);
        List<Comment> records = commentPage.getRecords();
        for (Comment record : records) {
            User courRent = userMapper.selectOne(new QueryWrapper<User>().eq("id", record.getUserId()));
            record.setUserName(courRent.getUserName());
            record.setUserAvatar(courRent.getAvatar());
        }
        return records;
    }
}
